﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace TheOliver.Controls
{
    public partial class ClipClapChar : UserControl
    {
        public ClipClapChar()
        {
            InitializeComponent();

            this.Foreground = new SolidColorBrush(Colors.White);
            this.FontSize = 64;

            this.Loaded += ClipClapChar_Loaded;

            _helper.Visibility = Visibility.Collapsed;

            _frontBottomGrid.Visibility = Visibility.Collapsed;
            _frontTopGrid.Visibility = Visibility.Collapsed;

            // To Do: Add Clippings dynamically 

            //Rectangle rect = new Rectangle();
            //rect.HorizontalAlignment = HorizontalAlignment.Stretch;
            //rect.VerticalAlignment = VerticalAlignment.Stretch;
            //rect.SetValue(Grid.RowProperty, 0);
            //rect.SetValue(Grid.ColumnProperty, 0);
            //rect.SetValue(Grid.RowSpanProperty, 1);
            //_frontTopGrid.Children.Add(rect);
            //_frontTopGrid.Clip = rect;
        }

        //protected override Size MeasureOverride(Size availableSize)
        //{
        //    if (_fixedSize)
        //    {
        //        availableSize = new Size(_charSizeWidth, _charSizeHeight);
        //    }
        //    return base.MeasureOverride(availableSize);
        //}

        //private bool _fixedSize = false;
        //public bool FixedSize
        //{
        //    get { return _fixedSize; }
        //    set 
        //    { 
        //        _fixedSize = value;
        //    }
        //}

        //private double _charSizeWidth = 50;
        //public double CharSizeWidth
        //{
        //    get { return _charSizeWidth; }
        //    set { _charSizeWidth = value; }
        //}

        //private double _charSizeHeight = 100;
        //public double CharSizeHeight
        //{
        //    get { return _charSizeHeight; }
        //    set { _charSizeHeight = value; }
        //}

        private string _nextCharacter;
        private string _character;
        public string Character
        {
            get { return _character; }
            set
            {
                _helper.Text = value;
                if (_character != value)
                {
                    if (_character == null)
                    {
                        _character = value;
                        InitBack(_character);
                    }
                    SetNextCharacter(value);
                }
            }
        }

        void ClipClapChar_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            _step1Storyboard.Completed += _step1Storyboard_Completed;
            _step2Storyboard.Completed += _step2Storyboard_Completed;
        }

        void _step2Storyboard_Completed(object sender, EventArgs e)
        {
            _backBottom.Source = _nextImage;
            _frontBottomGrid.Visibility = Visibility.Collapsed;
        }

        void _step1Storyboard_Completed(object sender, EventArgs e)
        {
            _frontTopGrid.Visibility = Visibility.Collapsed;
            _frontBottomGrid.Visibility = Visibility.Visible;

            _step1Storyboard.Stop();
            _step2Storyboard.Begin();
        }

        private void InitBack(string character)
        {
            WriteableBitmap back = GetBitmap(character);
            _backBottom.Source = back;
            _backTop.Source = back;
        }

        private void SetNextCharacter(string character)
        {
            if (character == null)
            {
                return;
            }

            _nextCharacter = character;

            // Next
            _nextImage = GetBitmap(_nextCharacter);
            _currentImage = GetBitmap(_character);

            _frontTop.Source = _currentImage;

            _frontTopGrid.Visibility = Visibility.Visible;
            _frontBottomGrid.Visibility = Visibility.Collapsed;

            _backTop.Source = _nextImage;
            _frontBottom.Source = _nextImage;
            
            _step1Storyboard.Begin();

            _character = character;
        }

        WriteableBitmap _currentImage;
        WriteableBitmap _nextImage;

        private WriteableBitmap GetBitmap(string character)
        {
            TextBlock tb = new TextBlock();
            tb.FontFamily = this.FontFamily;
            tb.FontSize = this.FontSize;
            tb.Foreground = this.Foreground;
            tb.Text = character;

            TranslateTransform tt = new TranslateTransform();
            tt.Y = tb.ActualHeight * -.15;

            // Next
            return new WriteableBitmap(tb, tt);
        }
        
    }
}